package com.sihan.swvp.common;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;  
import sun.misc.BASE64Encoder; 

import org.springframework.util.StringUtils;


/**
 * 
 * @author gsl
 *	md5工具类
 */
public class MD5Util {
    private static String sKey = "abcdef0123456789";  
    private static String ivParameter = "0123456789abcdef";  
		/**
		 * 32位加密算法
		 * @param str
		 */
		public static String MD5Encode(String str) {
				return dealStr(str, 2);
		}
		/**
		 * 16位加密算法
		 * @param str
		 */
		public static String MD5Encode2(String str) {
			return dealStr(str, 1);
		}
		/**
		 * @param str
		 * @param digit（1-16位，2-32位）
		 * @return
		 */
		private static String dealStr(String str,int digit) {
			if(StringUtils.isEmpty(str))return "";
			 MessageDigest md5 = null;
			 StringBuffer hexValue = new StringBuffer();
		        try {
		            md5 = MessageDigest.getInstance("MD5");
		            byte[] byteArray = str.getBytes("UTF-8");
			        byte[] md5Bytes = md5.digest(byteArray);
			        for (int i = 0; i < md5Bytes.length; i++) {
			            int val = ((int) md5Bytes[i]) & 0xff;
			            if (val < 16) {
			                hexValue.append("0");
			            }
			            hexValue.append(Integer.toHexString(val));
			        }
			        if(digit==1) {
			        	return hexValue.toString().substring(8,24);
			        }
		        } catch (Exception e) {
		            e.printStackTrace();
		            return "";
		        }
		        return hexValue.toString();
		}
		
		public static String reversibleMD5Encode(String str) {  
			String result = "";  
	        try {  
	            Cipher cipher;  
	            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");  
	            byte[] raw = sKey.getBytes();  
	            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");  
	            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());  
	            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);  
	            
	            byte[] encrypted = cipher.doFinal(str.getBytes("utf-8"));  
	            result = new BASE64Encoder().encode(encrypted);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }   
	        // 此处使用BASE64做转码。  
	        return result;  
		 }  
		
		public static String reversibleMD5Decode(String str) {  
			 try {  
		            byte[] raw = sKey.getBytes("ASCII");  
		            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");  
		            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");  
		            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());  
		            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);  
		            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(str);// 先用base64解密  
		            byte[] original = cipher.doFinal(encrypted1);  
		            String originalString = new String(original, "utf-8");  
		            return originalString;  
		        } catch (Exception ex) {  
		            ex.printStackTrace();  
		            return null;  
		        }   
	    }  
		
		public static void main(String[] args) {
			String str1 = MD5Util.reversibleMD5Encode("D:\\Program Files\\Photoshop7.0\\Photoshop.exe");
			String str2 = MD5Util.reversibleMD5Decode(str1);
			System.out.println("加密后："+str1);
			System.out.println("解密后："+str2);
		}
}
